const app = getApp();
import { baseUrl } from "../../../api/request"
import { getStorage } from "../../../utils/localStorage";
import { getMapDistance } from "../../../utils/address"
import { suggestInfo, vicinityHotSpots } from "../../../api/homePage"
Page({
    data: {
        navData: {
            level: 2,
            title: '乡镇景点'
        },
        imgUrl: baseUrl,
        swiper: {
            autoplay: true, //自动切换
            dots: true, //指示点
            dotsColor: "#ffffff", //指示点颜色
            dotsActiveColor: "#4646A0", //选中指示点颜色
            circular: true, //衔接滑动
            duration: 3000, //滑动动画时长
            boxFlag: false,
        },
        soFlag: false,
        dltObj: {},
        latlng: undefined,
        user: getStorage("user"),
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        venuList: [],
        type: null,
        //附近景点
        nearbyList: [],
    },
    onLoad(options) {
        //判断用户是否已经登录，否则跳转到登录页重新登录
        if (!getStorage("user")) {
            wx.redirectTo({
                url: '/pages/bootPage/index',
            });
        };
        /* 获取是否是从分享页进来的参数 true时 false不是 */
        app.globalData.sheetFlag = false;
        if (options.sheet) {
            app.globalData.sheetFlag = options.sheet;
        };
        this.setData({
            latlng: getStorage("latLng")
        });
        this.getInfo(options.id);
    },
    //获取详情信息
    getInfo(id) {
        const _this = this.data;
        suggestInfo({ landscapesId: id, longitude: _this.latlng.lng, latitude: _this.latlng.lat }).then(res => {
            if(res.data.data){
              res.data.data.imageScale = res.data.data.imageScale.split(",");
              res.data.data.distance = getMapDistance(this.data.latlng.lat, this.data.latlng.lng, res.data.data.latitude, res.data.data.longitude);
              this.setData({
                  dltObj: res.data.data
              });
            };
            this.getVicinityHotSpots(id);
        });
    },

    //获取附近景点
    getVicinityHotSpots(id) {
        const _this = this.data;
        vicinityHotSpots({ landscapesId: id, longitude: _this.latlng.lng, latitude: _this.latlng.lat }).then(res => {
            let arr = res.data.data.map(item => ({
                ...item,
                imageScale: item.imageScale ? item.imageScale.split(",") : []
            }));
            this.setData({
                nearbyList: arr
            });
        });
    },

    //附近景点列表点击事件
    fjjdListTap(e) {
        const item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url: '/pagesShouYe/pages/jingquDetail/index?id=' + item.landscapesId,
        });
        // this.getInfo(item.landscapesId);
        // this.getVicinityHotSpots(item.landscapesId);
    },

    //导航按钮点击事件
    daohangClick() {
        let _this = this.data;
        wx.openLocation({
            name:_this.dltObj.landscapesName,
            latitude: Number(_this.dltObj.latitude),
            longitude: Number(_this.dltObj.longitude),
        });
    },
    onShow() {
    },
    onReady() {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(res) {
        let that = this;
        return {
            title: that.data.dltObj.title,
            // imageUrl: that.data.detailObj[0],
            path: "pagesShouYe/pages/jingquDetail/index?id=" + that.data.dltObj.id + "&type=" + this.data.type + "&sheet=" + true,
            success: function (res) {
                console.log(res, "转发成功")
            },
            fail: function (res) {
                console.log(res, "转发失败")
            }
        }
    },
})